/**
 * Created by Administrator on 2017/2/2.
 */
var passwordEdit    = $('#password_edit'),
    passwordBtn     = $('#password-btn'),
    logoutBtn       = $('#logout-btn'),

    editId          = $('#edit-id'),
    editAccounts    = $('#edit-accounts'),
    editPassword    = $('#edit-password'),
    editNotPassword = $('#edit-notpassword'),
    details         = $('#details'),
    //在 index.js 设置工具栏
    editor_tool = [
        'source', '|',
        'formatblock', 'fontname', 'fontsize','|',
        'forecolor', 'hilitecolor', 'bold','italic', 'underline', 'link',
        'removeformat', '|',
        'justifyleft', 'justifycenter', 'justifyright', '|',
        'insertorderedlist', 'insertunorderedlist','|',
        'emoticons', 'image','baidumap','|',
        'fullscreen'
    ];

//后台 修改密码
passwordBtn.click(function () {
    passwordEdit.dialog('open');
});
//后台 登出系统
logoutBtn.click(function () {
    $.messager.confirm('操作提醒', '是否退出系统！', function (flag) {
        if (flag)
        {
            $.messager.progress({
                text : '登出系统中...',
            });
            location.href = ThinkPHP['MODULE'] + '/Login/logout';
        }
    });

});


$(function () {
    //后台 修改密码对话框
    passwordEdit.dialog({
        title: '修改密码',
        width: 370,
        height:280,
        iconCls:'icon-lock',
        closed: true,
        modal : true,
        maximizable : false,
        closable : true,
        draggable : false,
        buttons:[{
            text:'保存',
            size:'large',
            id: 'register-btn',
            iconCls:'icon-go',
            handler:function(){
                if (passwordEdit.form('validate')) {
                    $.ajax({
                        url: ThinkPHP['MODULE'] + '/Index/editPassword',
                        type: 'POST',
                        data: {
                            'id': editId.val(),
                            'accounts': editAccounts.val(),
                            'password': editPassword.val(),
                            'notpassword': editNotPassword.val(),
                        },
                        beforeSend: function () {
                            $.messager.progress({
                                'text': '正在尝修改密码中...'
                            });
                        },
                        success: function (data) {
                            $.messager.progress('close');
                            if (data > 0) {
                                $.messager.alert('修改密码提示', '帐号密码修改成功,请重新登录', 'info',function () {
                                    passwordEdit.form('reset');
                                    location.href = ThinkPHP['MODULE'] + '/Login/logout';
                                });
                            } else if (data == -1 || data == 0 ) {
                                $.messager.alert('修改密码提示', '密码被未修改，请输入新密码', 'warning');
                            } else {
                                $.messager.alert('修改密码提示', '未知错误，稍后重试...', 'warning');
                            }
                        }
                    });
                }

            }
        }],
        onOpen : function () {
            $(function ()
            {
                $('#register-btn').parent().css('text-align', 'center');
            });
        }
    });

    //详情页面dialog
    details.dialog({
        width: 780,
        height:500,
        iconCls:'icon-tip',
        closed: true,
        modal : true,
        maximizable : true,
        buttons:[{
            text:'关闭',
            size:'large',
            id: 'register-btn',
            iconCls:'icon-cross',
            handler:function(){
               details.dialog('close');

            }
        }],
    });

    //判断针对火狐浏览器，并判断easyui 渲染完毕后再隐藏遮罩
    if (navigator.userAgent.indexOf('Firefox') > 0)
    {
        $.parser.onComplete = function ()
        {
            $('.tabs-loading').hide();
        }
    }



});

$(window).resize(function () {
    passwordEdit.dialog('center');
})

$('#tabs').tabs({
    fit: true,
    border: false,
    onLoad : function () {
        //非火狐浏览器，移出掉 loading...
        if (navigator.userAgent.indexOf('Firefox') <= 0) {
            $('.tabs-loading').remove();
        }
    },
    onContextMenu: function (e,title,index) {
        e.preventDefault();
        var menu = $('#menu');
        var _this = this;

        //右击弹出菜单
        menu.menu('show', {
            top: e.pageY,
            left: e.pageX
        })

        if (index == 0) {
            menu.menu('disableItem', $('.closecur')[0]);
        } else {
            menu.menu('enableItem', $('.closecur')[0]);
        }

        //三个关闭方法
        menu.menu({
            onClick: function (item) {
                var tablist =  $(_this).tabs('tabs');
               switch (item.text) {
                   case '关闭':
                       $(_this).tabs('close', index);
                    break;
                   case '关闭所有':
                       for(var i=tablist.length; i>0; i--) {
                           $(_this).tabs('close', i);
                       }
                       break;
                   case '关闭其他':

                       for(var i=tablist.length; i>0; i--) {
                           if (i != index) {
                               $(_this).tabs('close', i);
                           }

                       }
                       $(_this).tabs('select',1);
                       break;
               }
            }
        })
    }
});
//左侧树型导航
$('#tree').tree({
    url: ThinkPHP['MODULE'] + '/Index/getTree',
    lines: true,
    animate: true,
    onClick: function (node) {
        var tabs = $('#tabs');

        //如果有链接才可以点击
        if (node.url) {
            if (tabs.tabs('exists',node.text)) {
                tabs.tabs('select',node.text);
            } else {
                switch (node.text) {
                    case '登录帐号':
                        $('#user-add').dialog('destroy');
                        $('#user-edit').dialog('destroy');
                        break;
                    case '职位部门':
                        $('#post-add').dialog('destroy');
                        $('#post-edit').dialog('destroy');
                        break;
                    case '员工档案':
                        $('#staff-add').dialog('destroy');
                        $('#staff-edit').dialog('destroy');
                        break;
                }
                //页面不存在，就给选项卡添加标签
                tabs.tabs('add', {
                    title: node.text,
                    closable: true,
                    iconCls: node.iconCls,
                    href: ThinkPHP['MODULE'] + '/'  +  node.url
                });
            }
        }
    }
});


//注册帐号验证
editAccounts.textbox({
    width: 220,
    height: 32,
});
//修改密码验证
editPassword.textbox({
    width: 220,
    height: 32,
    validType: 'length[6,30]',
    missingMessage: '请输入帐号密码',
    invalidMessage: '帐号密码6到30位'
});
//确认密码验证
editNotPassword.textbox({
    width: 220,
    height: 32,
    validType: 'equals["#edit-password"]',
    missingMessage: '请输入确认密码',
    invalidMessage: '确认密码和密码不一致'
});

//检查密码和确认密码是否相同
$.extend($.fn.validatebox.defaults.rules, {
    equals: {
        validator: function(value,param){
            return value == $(param[0]).val();
        },
        message: '确认密码和密码不一致'
    }
});

